﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace CommunitySystem.PageView
{
    public partial class KCSJControl : UserControl
    {
        InitKSSJ Kssj;
        public KCSJControl(InitKSSJ _kssj)
        {
            InitializeComponent();
            this.DataContext = _kssj;
            Kssj = _kssj;
            init();
        }
        private void init()
        {
            List<string> year = new List<string>();
            for (int i = DateTime.Now.Year; i <= DateTime.Now.Year+1; i++)
            {
                year.Add(i.ToString());
            }
            List<string> month = new List<string>();
            for (int i = 1; i <= 12; i++)
            {
                month.Add(i<10?("0"+i.ToString()):i.ToString());
            }
            List<string> day = new List<string>();
            for (int i = 1; i <= 31; i++)
            {
                day.Add(i < 10 ? ("0" + i.ToString()) : i.ToString());
            }
            List<string> hh = new List<string>();
            for (int i = 1; i <= 24; i++)
            {
                hh.Add(i < 10 ? ("0" + i.ToString()) : i.ToString());
            }
            List<string> mm = new List<string>();
            for (int i = 0; i <= 59; i++)
            {
                mm.Add(i < 10 ? ("0" + i.ToString()) : i.ToString());
            }

            this.cbyear.ItemsSource = year;
            this.cbmonth.ItemsSource = month;
            //this.cbday.ItemsSource = day;
            this.cbdhh.ItemsSource = hh;
            this.cbehh.ItemsSource = hh;
            this.cbemm.ItemsSource = mm;
            this.cbdmm.ItemsSource = mm;
        }

        private void cbmonth_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var year =Convert.ToInt32(this.cbyear.SelectedValue.ToString());
            var month=Convert.ToInt32(this.cbmonth.SelectedValue.ToString());
            var dd=31;
            if (year % 400 == 0 && month == 2)
                dd = 29;
            if (year % 400 != 0 && month == 2)
                dd = 28;
            List<string> day = new List<string>();
            for (int i = 1; i <= dd; i++)
            {
                day.Add(i < 10 ? ("0" + i.ToString()) : i.ToString());
            }
            this.cbday.ItemsSource = day;

            GetWeek();
        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.Visibility = Visibility.Collapsed;
        }

        private void cbyear_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            GetWeek();
        }

        private void cbday_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            GetWeek();
        }
        private void GetWeek()
        {
            if (this.cbyear.SelectedValue == null) return;
            if (this.cbmonth.SelectedValue == null) return;
            if (this.cbday.SelectedValue == null) return;
            var year = Convert.ToInt32(this.cbyear.SelectedValue.ToString());
            var month = Convert.ToInt32(this.cbmonth.SelectedValue.ToString());
            var day = Convert.ToInt32(this.cbday.SelectedValue.ToString());

            //DateTime.Parse(textBox1.Text.Trim()).DayOfWeek.ToString();   
            this.tbweek.Text=CaculateWeekDay(year, month, day);
        }

        private  string CaculateWeekDay(int y, int m, int d)
        {
            var ddd = y.ToString() + "-" + m.ToString() + "-" + d.ToString();
            return getCN(DateTime.Parse(ddd).DayOfWeek.ToString());
        }
        private string getCN(string en)
        {
            if (en.Equals("Monday"))
                return "星期一";
            else if (en.Equals("Tuesday"))
                return "星期二";
            else if (en.Equals("Wednesday"))
                return "星期三";
            else if (en.Equals("Thursday"))
                return "星期四";
            else if (en.Equals("Friday"))
                return "星期五";
            else if (en.Equals("Saturday"))
                return "星期六";
            else if (en.Equals("Sunday"))
                return "星期日";

            return "";

        }
        

    }
}
